﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Linq;
using System.Linq;
using System.Web;
using FacturIT.BLL.Model.Helper;
using FacturIT.BLL.Model.CompanyProject;

namespace FacturIT.Service.Repositories.CompanyProject
{
    internal class CompanyProjectRepository : IRepository<BLL.Model.CompanyProject.CompanyProject, int>
    {

        public CompanyProjectRepository()
        {
            DB = new CompanyProjectManagementDataContext(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["DatabaseToUse"]].ConnectionString);
        }

        protected Table<FacturIT.BLL.Model.CompanyProject.CompanyProject> DT
        {
            get { return ((CompanyProjectManagementDataContext)base.DB).CompanyProjects; }
        }

        public override FacturIT.BLL.Model.CompanyProject.CompanyProject Read(int id)
        {
            FacturIT.BLL.Model.CompanyProject.CompanyProject CompanyProject = ReadByID(id);

            return CompanyProject == null ? null : CompanyProject;
        }


        public override IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> ReadAll()
        {
            return from CompanyProject in DT
                   orderby CompanyProject.ProjectID
                   select CompanyProject;
        }


        internal IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> ReadAllCompanyProjectes(int user)
        {

            return from CompanyProject in DT
                   where CompanyProject.CompanyID.Equals(user)
                   orderby CompanyProject.CompanyProjectID
                   select CompanyProject;
        }

        public override IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> ReadAll(params int[] ids)
        {
            return from CompanyProject in DT
                   where ids.Contains(CompanyProject.CompanyProjectID)
                   select CompanyProject;
        }

        public override FacturIT.BLL.Model.CompanyProject.CompanyProject Add(FacturIT.BLL.Model.CompanyProject.CompanyProject t)
        {

            DT.InsertOnSubmit(t);
            SaveChanges();
            return Read(t.CompanyProjectID);
        }

        public override bool Delete(FacturIT.BLL.Model.CompanyProject.CompanyProject t)
        {

            DT.DeleteOnSubmit(t);
            SaveChanges();
            return !Exists(t.CompanyProjectID);
        }

        public override FacturIT.BLL.Model.CompanyProject.CompanyProject Update(FacturIT.BLL.Model.CompanyProject.CompanyProject t)
        {
            UpdateObject(t, ReadByID(t.CompanyProjectID), false);
            SaveChanges();
            return ReadByID(t.CompanyProjectID);
        }

        public override FacturIT.BLL.Model.CompanyProject.CompanyProject Duplicate(FacturIT.BLL.Model.CompanyProject.CompanyProject t)
        {
            return Add(Create(t.ProjectID, t.CompanyID));
        }

        protected override FacturIT.BLL.Model.CompanyProject.CompanyProject ReadByID(int id)
        {
            return DT.SingleOrDefault(x => x.CompanyProjectID == id);
        }

        protected override void UpdateObject(FacturIT.BLL.Model.CompanyProject.CompanyProject left, FacturIT.BLL.Model.CompanyProject.CompanyProject right, bool replaceID)
        {
            if (replaceID) left.CompanyProjectID = right.CompanyProjectID;
            left.ProjectID = right.ProjectID;
            left.CompanyID = right.CompanyID;
            
        }

        protected override IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> ApplyOrdering(IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> query, IDictionary<Func<FacturIT.BLL.Model.CompanyProject.CompanyProject, object>, Enum.Ordering> columns)
        {
            return OrderHelper.ApplyOrdering<FacturIT.BLL.Model.CompanyProject.CompanyProject, object>(query, columns, phone => phone.ProjectID);
        }
        public FacturIT.BLL.Model.CompanyProject.CompanyProject Create(int projectid, int CompanyID)
        {
            return new FacturIT.BLL.Model.CompanyProject.CompanyProject() { ProjectID = projectid, CompanyID = CompanyID };
        }

        internal BLL.Model.CompanyProject.CompanyProject ReadCompanyProjectByProjectAndCompany(int projectid, int CompanyID)
        {
            return (from CompanyProject in DT
                   where CompanyProject.ProjectID == projectid && CompanyProject.CompanyID == CompanyID
                   select CompanyProject).FirstOrDefault();
        }

        internal IEnumerable<FacturIT.BLL.Model.CompanyProject.CompanyProject> ReadAllCompaniesFromProject(int projectID)
        {
            return from CompanyProject in DT
                   where CompanyProject.ProjectID == projectID
                   select CompanyProject;
        }
    }
}